<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>查询构造器类 &mdash; CodeIgniter 3.0.0 中文手册|用户手册|用户指南|中文文档</title>
  

  
  
  <link rel="shortcut icon" href="../_static/ci-icon.ico" />
  

  
  <link href="../_static/css/fonts.css" rel="stylesheet" type="text/css" />

  
  
    

  

  
  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  
  <link rel="stylesheet" href="../_static/css/theme_cn.css" type="text/css" />
    <link rel="top" title="CodeIgniter 3.0.0 中文手册|用户手册|用户指南|中文文档" href="../index.html"/>
        <link rel="up" title="数据库参考" href="index.html"/>
        <link rel="next" title="事务" href="transactions.html"/>
        <link rel="prev" title="查询辅助函数" href="helpers.html"/> 

  
  <script src="../_static/js/modernizr.2.6.2.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div id="nav">
  <div id="nav_inner">
    
    
    
      <div id="pulldown-menu" class="ciNav">
        <ul>
<li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">欢迎使用 CodeIgniter</a><ul class="simple">
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">安装说明</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">下载 CodeIgniter</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/index.html">安装说明</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">从老版本升级</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">疑难解答</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter 概览</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">开始</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter 是什么？</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/features.html">支持特性</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">应用程序流程图</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">模型 - 视图 - 控制器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">架构目标</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">教程 - 内容提要</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">加载静态内容</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">读取新闻条目</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">创建新闻条目</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">结束语</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">向 CodeIgniter 贡献你的力量</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">编写 CodeIgniter 的文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer's Certificate of Origin 1.1</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">常规主题</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URL</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">控制器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">保留名称</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/views.html">视图</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/models.html">模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">使用 CodeIgniter 类库</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">创建类库</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">使用 CodeIgniter 驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">创建驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">创建核心系统类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">创建附属类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">钩子 - 扩展框架核心</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">自动加载资源</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">公共函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">兼容性函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI 路由</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">错误处理</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">网页缓存</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">程序分析</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/cli.html">以 CLI 方式运行</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">管理你的应用程序</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">处理多环境</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">在视图文件中使用 PHP 替代语法</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/security.html">安全</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP 开发规范</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">类库参考</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">基准测试类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">缓存驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/calendar.html">日历类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">购物车类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">配置类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encrypt.html">加密类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">加密类（新版）</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">文件上传类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">表单验证类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">图像处理类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">输入类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">语言类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">加载器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">迁移类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">输出类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">分页类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">模板解析类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">安全类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/table.html">HTML 表格类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">引用通告类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">排版类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">单元测试类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">用户代理类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/xmlrpc.html">XML-RPC 与 XML-RPC 服务器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip 编码类</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">数据库参考</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="examples.html">数据库快速入门: 示例代码</a></li>
<li class="toctree-l2"><a class="reference internal" href="configuration.html">数据库配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="connecting.html">连接数据库</a></li>
<li class="toctree-l2"><a class="reference internal" href="queries.html">查询</a></li>
<li class="toctree-l2"><a class="reference internal" href="results.html">生成查询结果</a></li>
<li class="toctree-l2"><a class="reference internal" href="helpers.html">查询辅助函数</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">查询构造器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="transactions.html">事务</a></li>
<li class="toctree-l2"><a class="reference internal" href="metadata.html">获取元数据</a></li>
<li class="toctree-l2"><a class="reference internal" href="call_function.html">自定义函数调用</a></li>
<li class="toctree-l2"><a class="reference internal" href="caching.html">查询缓存</a></li>
<li class="toctree-l2"><a class="reference internal" href="forge.html">使用 Database Forge 维护数据库</a></li>
<li class="toctree-l2"><a class="reference internal" href="utilities.html">数据库工具类</a></li>
<li class="toctree-l2"><a class="reference internal" href="db_driver_reference.html">数据库驱动器参考</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">辅助函数参考</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">数组辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">验证码辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">日期辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">目录辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">下载辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">邮件辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">文件辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">表单辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">语言辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">数字辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">路径辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">安全辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">表情辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">字符串辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">文本辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">排版辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML 辅助函数</a></li>
</ul>
</li>
</ul>

      </div>
    
      
  </div>
</div>
<div id="nav2">
  <a href="#" id="openToc">
    <img src="" title="切换目录" alt="切换目录" />
  </a>
</div>

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-nav-search">
        
          <a href="../index.html" class="fa fa-home"> CodeIgniter</a>
        
        
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="搜索手册" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
        
          
          
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">欢迎使用 CodeIgniter</a><ul class="simple">
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">安装说明</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">下载 CodeIgniter</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/index.html">安装说明</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">从老版本升级</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">疑难解答</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter 概览</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">开始</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter 是什么？</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/features.html">支持特性</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">应用程序流程图</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">模型 - 视图 - 控制器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">架构目标</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">教程 - 内容提要</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">加载静态内容</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">读取新闻条目</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">创建新闻条目</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">结束语</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">向 CodeIgniter 贡献你的力量</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">编写 CodeIgniter 的文档</a></li>
<li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer's Certificate of Origin 1.1</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">常规主题</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URL</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">控制器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">保留名称</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/views.html">视图</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/models.html">模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">使用 CodeIgniter 类库</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">创建类库</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">使用 CodeIgniter 驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">创建驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">创建核心系统类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">创建附属类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">钩子 - 扩展框架核心</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">自动加载资源</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">公共函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">兼容性函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI 路由</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">错误处理</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">网页缓存</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">程序分析</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/cli.html">以 CLI 方式运行</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">管理你的应用程序</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">处理多环境</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">在视图文件中使用 PHP 替代语法</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/security.html">安全</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP 开发规范</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">类库参考</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">基准测试类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">缓存驱动器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/calendar.html">日历类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">购物车类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">配置类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encrypt.html">加密类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">加密类（新版）</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">文件上传类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">表单验证类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">图像处理类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">输入类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">语言类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">加载器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">迁移类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">输出类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">分页类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">模板解析类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">安全类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/table.html">HTML 表格类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">引用通告类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">排版类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">单元测试类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI 类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">用户代理类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/xmlrpc.html">XML-RPC 与 XML-RPC 服务器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip 编码类</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">数据库参考</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="examples.html">数据库快速入门: 示例代码</a></li>
<li class="toctree-l2"><a class="reference internal" href="configuration.html">数据库配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="connecting.html">连接数据库</a></li>
<li class="toctree-l2"><a class="reference internal" href="queries.html">查询</a></li>
<li class="toctree-l2"><a class="reference internal" href="results.html">生成查询结果</a></li>
<li class="toctree-l2"><a class="reference internal" href="helpers.html">查询辅助函数</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">查询构造器类</a></li>
<li class="toctree-l2"><a class="reference internal" href="transactions.html">事务</a></li>
<li class="toctree-l2"><a class="reference internal" href="metadata.html">获取元数据</a></li>
<li class="toctree-l2"><a class="reference internal" href="call_function.html">自定义函数调用</a></li>
<li class="toctree-l2"><a class="reference internal" href="caching.html">查询缓存</a></li>
<li class="toctree-l2"><a class="reference internal" href="forge.html">使用 Database Forge 维护数据库</a></li>
<li class="toctree-l2"><a class="reference internal" href="utilities.html">数据库工具类</a></li>
<li class="toctree-l2"><a class="reference internal" href="db_driver_reference.html">数据库驱动器参考</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">辅助函数参考</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">数组辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">验证码辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">日期辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">目录辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">下载辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">邮件辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">文件辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">表单辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">语言辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">数字辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">路径辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">安全辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">表情辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">字符串辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">文本辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">排版辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL 辅助函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML 辅助函数</a></li>
</ul>
</li>
</ul>

          
        
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">CodeIgniter</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">首页</a> &raquo; </li>
    
    <li><a href="index.html">数据库参考</a> &raquo; </li>
    
    <li>查询构造器类</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
    <div style="float:right;">
      <div style="text-decoration:underline;color:blue;margin-left:5px;cursor:pointer;" id="closeMe">
        <img title="目录" alt="toc" src="" />
      </div>
    </div>
  </ul>
  <hr/>
</div>
          <div role="main" class="document">
            
  <div class="section" id="id1">
<h1>查询构造器类<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h1>
<p>CodeIgniter 提供了查询构造器类，查询构造器允许你使用较少的代码来在数据库中
获取、新增或更新数据。有时只需要一两行代码就能完成数据库操作。CodeIgniter
并不需要为每个数据表提供一个类，而是使用了一种更简单的接口。</p>
<p>除了简单，使用查询构造器的另一个好处是可以让你创建数据库独立的应用程序，
这是因为查询语句是由每个独立的数据库适配器生成的。另外，由于系统会自动对数据
进行转义，所以它还能提供更安全的查询。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果你想要编写你自己的查询语句，你可以在数据库配置文件中禁用这个类，
这样数据库核心类库和适配器将使用更少的资源。</p>
</div>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#id2" id="id14">查询</a></li>
<li><a class="reference internal" href="#id3" id="id15">搜索</a></li>
<li><a class="reference internal" href="#id4" id="id16">模糊搜索</a></li>
<li><a class="reference internal" href="#id5" id="id17">排序</a></li>
<li><a class="reference internal" href="#id6" id="id18">分页与计数</a></li>
<li><a class="reference internal" href="#id7" id="id19">查询条件组</a></li>
<li><a class="reference internal" href="#id8" id="id20">插入数据</a></li>
<li><a class="reference internal" href="#id9" id="id21">更新数据</a></li>
<li><a class="reference internal" href="#id10" id="id22">删除数据</a></li>
<li><a class="reference internal" href="#id11" id="id23">链式方法</a></li>
<li><a class="reference internal" href="#ar-caching" id="id24">查询构造器缓存</a></li>
<li><a class="reference internal" href="#id13" id="id25">重置查询构造器</a></li>
<li><a class="reference internal" href="#class-reference" id="id26">Class Reference</a></li>
</ul>
</div>
<div class="section" id="id2">
<h2><a class="toc-backref" href="#id14">查询</a><a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h2>
<p>下面的方法用来构建 <strong>SELECT</strong> 语句。</p>
<p><strong>$this-&gt;db-&gt;get()</strong></p>
<p>该方法执行 SELECT 语句并返回查询结果，可以得到一个表的所有数据:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>  <span class="c1">// Produces: SELECT * FROM mytable</span>
</pre></div>
</div>
<p>第二和第三个参数用于设置 LIMIT 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span>

<span class="c1">// Executes: SELECT * FROM mytable LIMIT 20, 10</span>
<span class="c1">// (in MySQL. Other databases have slightly different syntax)</span>
</pre></div>
</div>
<p>你应该已经注意到了，上面的方法的结果都赋值给了一个 $query 变量，通过这个变量，
我们可以得到查询的结果:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>

<span class="k">foreach</span> <span class="p">(</span><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">result</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$row</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$row</span><span class="o">-&gt;</span><span class="na">title</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>参考 <a class="reference internal" href="results.html"><em>生成查询结果</em></a> 页面获取关于生成结果的更多信息。</p>
<p><strong>$this-&gt;db-&gt;get_compiled_select()</strong></p>
<p>该方法和 <strong>$this-&gt;db-&gt;get()</strong> 方法一样编译 SELECT 查询并返回查询的 SQL 语句，
但是，该方法并不执行它。</p>
<p>例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get_compiled_select</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$sql</span><span class="p">;</span>

<span class="c1">// Prints string: SELECT * FROM mytable</span>
</pre></div>
</div>
<p>第二个参数用于设置是否重置查询（默认会重置，和使用 <cite>$this-&gt;db-&gt;get()</cite> 方法时一样）:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">20</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get_compiled_select</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>

<span class="c1">// Prints string: SELECT * FROM mytable LIMIT 20, 10</span>
<span class="c1">// (in MySQL. Other databases have slightly different syntax)</span>

<span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;title, content, date&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get_compiled_select</span><span class="p">();</span>

<span class="c1">// Prints string: SELECT title, content, date FROM mytable LIMIT 20, 10</span>
</pre></div>
</div>
<p>上面的例子中，最值得注意的是，第二个查询并没有用到 <strong>$this-&gt;db-&gt;from()</strong> 方法，
也没有为查询指定表名参数，但是它生成的 SQL 语句中有 FROM mytable 子句。
这是因为查询并没有被重置（使用 <strong>$this-&gt;db-&gt;get()</strong> 方法查询会被执行并被重置，
使用 <strong>$this-&gt;db-&gt;reset_query()</strong> 方法直接重置）。</p>
<p><strong>$this-&gt;db-&gt;get_where()</strong></p>
<p>这个方法基本上和上面的方法一样，但它提供了第二个参数可以让你添加一个 WHERE 子句，
而不是使用 <cite>db-&gt;where()</cite> 方法:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get_where</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">),</span> <span class="nv">$limit</span><span class="p">,</span> <span class="nv">$offset</span><span class="p">);</span>
</pre></div>
</div>
<p>阅读下面的 <cite>db-&gt;where()</cite> 方法获取更多信息。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">get_where() 方法的前身为 getwhere(), 已废除</p>
</div>
<p><strong>$this-&gt;db-&gt;select()</strong></p>
<p>该方法用于编写查询语句中的 SELECT 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;title, content, date&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>

<span class="c1">// Executes: SELECT title, content, date FROM mytable</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果你要查询表的所有列，可以不用写这个函数，CodeIgniter 会自动查询所有列（SELECT *）。</p>
</div>
<p><tt class="docutils literal"><span class="pre">$this-&gt;db-&gt;select()</span></tt> 方法的第二个参数可选，如果设置为 FALSE，CodeIgniter 将不保护你的
表名和字段名，这在当你编写复合查询语句时很有用，不会破坏你编写的语句。</p>
<div class="highlight-ci"><div class="highlight"><pre>$this-&gt;db-&gt;select(&#39;(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4&#39;) AS amount_paid&#39;, FALSE);
$query = $this-&gt;db-&gt;get(&#39;mytable&#39;);
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;select_max()</strong></p>
<p>该方法用于编写查询语句中的 <tt class="docutils literal"><span class="pre">SELECT</span> <span class="pre">MAX(field)</span></tt> 部分，你可以使用第二个参数（可选）重命名结果字段。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select_max</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">);</span>  <span class="c1">// Produces: SELECT MAX(age) as age FROM members</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select_max</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">,</span> <span class="s1">&#39;member_age&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">);</span> <span class="c1">// Produces: SELECT MAX(age) as member_age FROM members</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;select_min()</strong></p>
<p>该方法用于编写查询语句中的 <tt class="docutils literal"><span class="pre">SELECT</span> <span class="pre">MIN(field)</span></tt> 部分，和 select_max() 方法一样，
你可以使用第二个参数（可选）重命名结果字段。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select_min</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">);</span> <span class="c1">// Produces: SELECT MIN(age) as age FROM members</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;select_avg()</strong></p>
<p>该方法用于编写查询语句中的 <tt class="docutils literal"><span class="pre">SELECT</span> <span class="pre">AVG(field)</span></tt> 部分，和 select_max() 方法一样，
你可以使用第二个参数（可选）重命名结果字段。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select_avg</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">);</span> <span class="c1">// Produces: SELECT AVG(age) as age FROM members</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;select_sum()</strong></p>
<p>该方法用于编写查询语句中的 <tt class="docutils literal"><span class="pre">SELECT</span> <span class="pre">SUM(field)</span></tt> 部分，和 select_max() 方法一样，
你可以使用第二个参数（可选）重命名结果字段。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select_sum</span><span class="p">(</span><span class="s1">&#39;age&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;members&#39;</span><span class="p">);</span> <span class="c1">// Produces: SELECT SUM(age) as age FROM members</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;from()</strong></p>
<p>该方法用于编写查询语句中的 FROM 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;title, content, date&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">();</span>  <span class="c1">// Produces: SELECT title, content, date FROM mytable</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">正如前面所说，查询中的 FROM 部分可以在方法 $this-&gt;db-&gt;get() 中指定，所以，你可以
选择任意一种你喜欢的方式。</p>
</div>
<p><strong>$this-&gt;db-&gt;join()</strong></p>
<p>该方法用于编写查询语句中的 JOIN 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="s1">&#39;blogs&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">join</span><span class="p">(</span><span class="s1">&#39;comments&#39;</span><span class="p">,</span> <span class="s1">&#39;comments.id = blogs.id&#39;</span><span class="p">);</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">();</span>

<span class="c1">// Produces:</span>
<span class="c1">// SELECT * FROM blogs JOIN comments ON comments.id = blogs.id</span>
</pre></div>
</div>
<p>如果你的查询中有多个连接，你可以多次调用这个方法。</p>
<p>你可以传入第三个参数指定连接的类型，有这样几种选择：left，right，outer，inner，left
outer 和 right outer 。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">join</span><span class="p">(</span><span class="s1">&#39;comments&#39;</span><span class="p">,</span> <span class="s1">&#39;comments.id = blogs.id&#39;</span><span class="p">,</span> <span class="s1">&#39;left&#39;</span><span class="p">);</span>
<span class="c1">// Produces: LEFT JOIN comments ON comments.id = blogs.id</span>
</pre></div>
</div>
</div>
<div class="section" id="id3">
<h2><a class="toc-backref" href="#id15">搜索</a><a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;where()</strong></p>
<p>该方法提供了4中方式让你编写查询语句中的 WHERE 子句：</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有的数据将会自动转义，生成安全的查询语句。</p>
</div>
<ol class="arabic">
<li><p class="first"><strong>简单的 key/value 方式:</strong></p>
<blockquote>
<div><div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span> <span class="c1">// Produces: WHERE name = &#39;Joe&#39;</span>
</pre></div>
</div>
<p>注意自动为你加上了等号。</p>
<p>如果你多次调用该方法，那么多个 WHERE 条件将会使用 AND 连接起来：</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="nv">$title</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;status&#39;</span><span class="p">,</span> <span class="nv">$status</span><span class="p">);</span>
<span class="c1">// WHERE name = &#39;Joe&#39; AND title = &#39;boss&#39; AND status = &#39;active&#39;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>自定义 key/value 方式:</strong></p>
<blockquote>
<div><p>为了控制比较，你可以在第一个参数中包含一个比较运算符：</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;name !=&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id &lt;&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span> <span class="c1">// Produces: WHERE name != &#39;Joe&#39; AND id &lt; 45</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>关联数组方式:</strong></p>
<blockquote>
<div><div class="highlight-ci"><div class="highlight"><pre><span class="nv">$array</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="nv">$name</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$title</span><span class="p">,</span> <span class="s1">&#39;status&#39;</span> <span class="o">=&gt;</span> <span class="nv">$status</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
<span class="c1">// Produces: WHERE name = &#39;Joe&#39; AND title = &#39;boss&#39; AND status = &#39;active&#39;</span>
</pre></div>
</div>
<p>你也可以在这个方法里包含你自己的比较运算符：</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$array</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;name !=&#39;</span> <span class="o">=&gt;</span> <span class="nv">$name</span><span class="p">,</span> <span class="s1">&#39;id &lt;&#39;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">,</span> <span class="s1">&#39;date &gt;&#39;</span> <span class="o">=&gt;</span> <span class="nv">$date</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>自定义字符串:</strong></p>
<blockquote>
<div><p>你可以完全手工编写 WHERE 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$where</span> <span class="o">=</span> <span class="s2">&quot;name=&#39;Joe&#39; AND status=&#39;boss&#39; OR status=&#39;active&#39;&quot;</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="nv">$where</span><span class="p">);</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ol>
<p><tt class="docutils literal"><span class="pre">$this-&gt;db-&gt;where()</span></tt> 方法有一个可选的第三个参数，如果设置为 FALSE，CodeIgniter
将不保护你的表名和字段名。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;MATCH (field) AGAINST (&quot;value&quot;)&#39;</span><span class="p">,</span> <span class="k">NULL</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;or_where()</strong></p>
<p>这个方法和上面的方法一样，只是多个 WHERE 条件之间使用 OR 进行连接:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;name !=&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">or_where</span><span class="p">(</span><span class="s1">&#39;id &gt;&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span>  <span class="c1">// Produces: WHERE name != &#39;Joe&#39; OR id &gt; 50</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">or_where() 方法的前身为 orwhere(), 已废除</p>
</div>
<p><strong>$this-&gt;db-&gt;where_in()</strong></p>
<p>该方法用于生成 WHERE IN 子句，多个子句之间使用 AND 连接</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$names</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Frank&#39;</span><span class="p">,</span> <span class="s1">&#39;Todd&#39;</span><span class="p">,</span> <span class="s1">&#39;James&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where_in</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="nv">$names</span><span class="p">);</span>
<span class="c1">// Produces: WHERE username IN (&#39;Frank&#39;, &#39;Todd&#39;, &#39;James&#39;)</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;or_where_in()</strong></p>
<p>该方法用于生成 WHERE IN 子句，多个子句之间使用 OR 连接</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$names</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Frank&#39;</span><span class="p">,</span> <span class="s1">&#39;Todd&#39;</span><span class="p">,</span> <span class="s1">&#39;James&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">or_where_in</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="nv">$names</span><span class="p">);</span>
<span class="c1">// Produces: OR username IN (&#39;Frank&#39;, &#39;Todd&#39;, &#39;James&#39;)</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;where_not_in()</strong></p>
<p>该方法用于生成 WHERE NOT IN 子句，多个子句之间使用 AND 连接</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$names</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Frank&#39;</span><span class="p">,</span> <span class="s1">&#39;Todd&#39;</span><span class="p">,</span> <span class="s1">&#39;James&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where_not_in</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="nv">$names</span><span class="p">);</span>
<span class="c1">// Produces: WHERE username NOT IN (&#39;Frank&#39;, &#39;Todd&#39;, &#39;James&#39;)</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;or_where_not_in()</strong></p>
<p>该方法用于生成 WHERE NOT IN 子句，多个子句之间使用 OR 连接</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$names</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;Frank&#39;</span><span class="p">,</span> <span class="s1">&#39;Todd&#39;</span><span class="p">,</span> <span class="s1">&#39;James&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">or_where_not_in</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="nv">$names</span><span class="p">);</span>
<span class="c1">// Produces: OR username NOT IN (&#39;Frank&#39;, &#39;Todd&#39;, &#39;James&#39;)</span>
</pre></div>
</div>
</div>
<div class="section" id="id4">
<h2><a class="toc-backref" href="#id16">模糊搜索</a><a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;like()</strong></p>
<p>该方法用于生成 LIKE 子句，在进行搜索时非常有用。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有数据将会自动被转义。</p>
</div>
<ol class="arabic">
<li><p class="first"><strong>简单 key/value 方式:</strong></p>
<blockquote>
<div><div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="c1">// Produces: WHERE `title` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
<p>如果你多次调用该方法，那么多个 WHERE 条件将会使用 AND 连接起来:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="c1">// WHERE `title` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39; AND  `body` LIKE &#39;%match% ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
<p>可以传入第三个可选的参数来控制 LIKE 通配符（%）的位置，可用选项有：'before'，'after' 和
'both' (默认为 'both')。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">,</span> <span class="s1">&#39;before&#39;</span><span class="p">);</span>    <span class="c1">// Produces: WHERE `title` LIKE &#39;%match&#39; ESCAPE &#39;!&#39;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">,</span> <span class="s1">&#39;after&#39;</span><span class="p">);</span> <span class="c1">// Produces: WHERE `title` LIKE &#39;match%&#39; ESCAPE &#39;!&#39;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">,</span> <span class="s1">&#39;both&#39;</span><span class="p">);</span>  <span class="c1">// Produces: WHERE `title` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first"><strong>关联数组方式:</strong></p>
<blockquote>
<div><div class="highlight-ci"><div class="highlight"><pre><span class="nv">$array</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$match</span><span class="p">,</span> <span class="s1">&#39;page1&#39;</span> <span class="o">=&gt;</span> <span class="nv">$match</span><span class="p">,</span> <span class="s1">&#39;page2&#39;</span> <span class="o">=&gt;</span> <span class="nv">$match</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
<span class="c1">// WHERE `title` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39; AND  `page1` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39; AND  `page2` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
</div></blockquote>
</li>
</ol>
<p><strong>$this-&gt;db-&gt;or_like()</strong></p>
<p>这个方法和上面的方法一样，只是多个 WHERE 条件之间使用 OR 进行连接:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">or_like</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="nv">$match</span><span class="p">);</span>
<span class="c1">// WHERE `title` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39; OR  `body` LIKE &#39;%match%&#39; ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last"><tt class="docutils literal"><span class="pre">or_like()</span></tt> 方法的前身为 <tt class="docutils literal"><span class="pre">orlike()</span></tt>, 已废除</p>
</div>
<p><strong>$this-&gt;db-&gt;not_like()</strong></p>
<p>这个方法和 <tt class="docutils literal"><span class="pre">like()</span></tt> 方法一样，只是生成 NOT LIKE 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">not_like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>  <span class="c1">// WHERE `title` NOT LIKE &#39;%match% ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;or_not_like()</strong></p>
<p>这个方法和 <tt class="docutils literal"><span class="pre">not_like()</span></tt> 方法一样，只是多个 WHERE 条件之间使用 OR 进行连接:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">or_not_like</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="c1">// WHERE `title` LIKE &#39;%match% OR  `body` NOT LIKE &#39;%match%&#39; ESCAPE &#39;!&#39;</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;group_by()</strong></p>
<p>该方法用于生成 GROUP BY 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">group_by</span><span class="p">(</span><span class="s2">&quot;title&quot;</span><span class="p">);</span> <span class="c1">// Produces: GROUP BY title</span>
</pre></div>
</div>
<p>你也可以通过一个数组传入多个值:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">group_by</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s2">&quot;title&quot;</span><span class="p">,</span> <span class="s2">&quot;date&quot;</span><span class="p">));</span>  <span class="c1">// Produces: GROUP BY title, date</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">group_by() 方法前身为 groupby(), 已废除</p>
</div>
<p><strong>$this-&gt;db-&gt;distinct()</strong></p>
<p>该方法用于向查询中添加 DISTINCT 关键字：</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">distinct</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;table&#39;</span><span class="p">);</span> <span class="c1">// Produces: SELECT DISTINCT * FROM table</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;having()</strong></p>
<p>该方法用于生成 HAVING 子句，有下面两种不同的语法:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">having</span><span class="p">(</span><span class="s1">&#39;user_id = 45&#39;</span><span class="p">);</span>  <span class="c1">// Produces: HAVING user_id = 45</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">having</span><span class="p">(</span><span class="s1">&#39;user_id&#39;</span><span class="p">,</span>  <span class="mi">45</span><span class="p">);</span>  <span class="c1">// Produces: HAVING user_id = 45</span>
</pre></div>
</div>
<p>你也可以通过一个数组传入多个值:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">having</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;title =&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Title&#39;</span><span class="p">,</span> <span class="s1">&#39;id &lt;&#39;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">));</span>
<span class="c1">// Produces: HAVING title = &#39;My Title&#39;, id &lt; 45</span>
</pre></div>
</div>
<p>如果 CodeIgniter 自动转义你的查询，为了避免转义，你可以将第三个参数设置为 FALSE 。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">having</span><span class="p">(</span><span class="s1">&#39;user_id&#39;</span><span class="p">,</span>  <span class="mi">45</span><span class="p">);</span>  <span class="c1">// Produces: HAVING `user_id` = 45 in some databases such as MySQL</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">having</span><span class="p">(</span><span class="s1">&#39;user_id&#39;</span><span class="p">,</span>  <span class="mi">45</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>  <span class="c1">// Produces: HAVING user_id = 45</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;or_having()</strong></p>
<p>该方法和 <tt class="docutils literal"><span class="pre">having()</span></tt> 方法一样，只是多个条件之间使用 OR 进行连接。</p>
</div>
<div class="section" id="id5">
<h2><a class="toc-backref" href="#id17">排序</a><a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;order_by()</strong></p>
<p>该方法用于生成 ORDER BY 子句。</p>
<p>第一个参数为你想要排序的字段名，第二个参数用于设置排序的方向，
可选项有： ASC（升序），DESC（降序）和 RANDOM （随机）。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;DESC&#39;</span><span class="p">);</span>
<span class="c1">// Produces: ORDER BY `title` DESC</span>
</pre></div>
</div>
<p>第一个参数也可以是你自己的排序字符串:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="s1">&#39;title DESC, name ASC&#39;</span><span class="p">);</span>
<span class="c1">// Produces: ORDER BY `title` DESC, `name` ASC</span>
</pre></div>
</div>
<p>如果需要根据多个字段进行排序，可以多次调用该方法。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;DESC&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;ASC&#39;</span><span class="p">);</span>
<span class="c1">// Produces: ORDER BY `title` DESC, `name` ASC</span>
</pre></div>
</div>
<p>如果你选择了 <strong>RANDOM</strong> （随机排序），第一个参数会被忽略，但是你可以传入一个
数字值，作为随机数的 seed。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;RANDOM&#39;</span><span class="p">);</span>
<span class="c1">// Produces: ORDER BY RAND()</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">order_by</span><span class="p">(</span><span class="mi">42</span><span class="p">,</span> <span class="s1">&#39;RANDOM&#39;</span><span class="p">);</span>
<span class="c1">// Produces: ORDER BY RAND(42)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">order_by() 方法的前身为 orderby(), 已废除</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">Oracle 暂时还不支持随机排序，会默认使用升序</p>
</div>
</div>
<div class="section" id="id6">
<h2><a class="toc-backref" href="#id18">分页与计数</a><a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;limit()</strong></p>
<p>该方法用于限制你的查询返回结果的数量:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>  <span class="c1">// Produces: LIMIT 10</span>
</pre></div>
</div>
<p>第二个参数可以用来设置偏移。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="c1">// Produces: LIMIT 20, 10 (in MySQL.  Other databases have slightly different syntax)</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;count_all_results()</strong></p>
<p>该方法用于获取特定查询返回结果的数量，也可以使用查询构造器的这些方法：
<tt class="docutils literal"><span class="pre">where()</span></tt>，<tt class="docutils literal"><span class="pre">or_where()</span></tt>，<tt class="docutils literal"><span class="pre">like()</span></tt>，<tt class="docutils literal"><span class="pre">or_like()</span></tt> 等等。举例:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">count_all_results</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">);</span>  <span class="c1">// Produces an integer, like 25</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">like</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;match&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">count_all_results</span><span class="p">();</span> <span class="c1">// Produces an integer, like 17</span>
</pre></div>
</div>
<p>但是，这个方法会重置你在 <tt class="docutils literal"><span class="pre">select()</span></tt> 方法里设置的所有值，如果你希望保留它们，可以将
第二个参数设置为 FALSE</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">count_all_results</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;count_all()</strong></p>
<p>该方法用于获取某个表的总行数，第一个参数为表名:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">count_all</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">);</span>  <span class="c1">// Produces an integer, like 25</span>
</pre></div>
</div>
</div>
<div class="section" id="id7">
<h2><a class="toc-backref" href="#id19">查询条件组</a><a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<p>查询条件组可以让你生成用括号括起来的一组 WHERE 条件，这能创造出非常复杂的 WHERE 子句，
支持嵌套的条件组。例如:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="s1">&#39;my_table&#39;</span><span class="p">)</span>
    <span class="o">-&gt;</span><span class="na">group_start</span><span class="p">()</span>
        <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">)</span>
        <span class="o">-&gt;</span><span class="na">or_group_start</span><span class="p">()</span>
            <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">)</span>
            <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
        <span class="o">-&gt;</span><span class="na">group_end</span><span class="p">()</span>
    <span class="o">-&gt;</span><span class="na">group_end</span><span class="p">()</span>
    <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">get</span><span class="p">();</span>

<span class="c1">// Generates:</span>
<span class="c1">// SELECT * FROM (`my_table`) WHERE ( `a` = &#39;a&#39; OR ( `b` = &#39;b&#39; AND `c` = &#39;c&#39; ) ) AND `d` = &#39;d&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">条件组必须要配对，确保每个 group_start() 方法都有一个 group_end() 方法与之配对。</p>
</div>
<p><strong>$this-&gt;db-&gt;group_start()</strong></p>
<p>开始一个新的条件组，为查询中的 WHERE 条件添加一个左括号。</p>
<p><strong>$this-&gt;db-&gt;or_group_start()</strong></p>
<p>开始一个新的条件组，为查询中的 WHERE 条件添加一个左括号，并在前面加上 OR 。</p>
<p><strong>$this-&gt;db-&gt;not_group_start()</strong></p>
<p>开始一个新的条件组，为查询中的 WHERE 条件添加一个左括号，并在前面加上 NOT 。</p>
<p><strong>$this-&gt;db-&gt;or_not_group_start()</strong></p>
<p>开始一个新的条件组，为查询中的 WHERE 条件添加一个左括号，并在前面加上 OR NOT 。</p>
<p><strong>$this-&gt;db-&gt;group_end()</strong></p>
<p>结束当前的条件组，为查询中的 WHERE 条件添加一个右括号。</p>
</div>
<div class="section" id="id8">
<h2><a class="toc-backref" href="#id20">插入数据</a><a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;insert()</strong></p>
<p>该方法根据你提供的数据生成一条 INSERT 语句并执行，它的参数是一个**数组**
或一个**对象**，下面是使用数组的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Name&#39;</span><span class="p">,</span>
    <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My date&#39;</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
<span class="c1">// Produces: INSERT INTO mytable (title, name, date) VALUES (&#39;My title&#39;, &#39;My name&#39;, &#39;My date&#39;)</span>
</pre></div>
</div>
<p>第一个参数为要插入的表名，第二个参数为要插入的数据，是个关联数组。</p>
<p>下面是使用对象的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">class Myclass {</span>
<span class="cm">    public $title = &#39;My Title&#39;;</span>
<span class="cm">    public $content = &#39;My Content&#39;;</span>
<span class="cm">    public $date = &#39;My Date&#39;;</span>
<span class="cm">}</span>
<span class="cm">*/</span>

<span class="nv">$object</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Myclass</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$object</span><span class="p">);</span>
<span class="c1">// Produces: INSERT INTO mytable (title, content, date) VALUES (&#39;My Title&#39;, &#39;My Content&#39;, &#39;My Date&#39;)</span>
</pre></div>
</div>
<p>第一个参数为要插入的表名，第二个参数为要插入的数据，是个对象。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有数据会被自动转义，生成安全的查询语句。</p>
</div>
<p><strong>$this-&gt;db-&gt;get_compiled_insert()</strong></p>
<p>该方法和 $this-&gt;db-&gt;insert() 方法一样根据你提供的数据生成一条 INSERT 语句，但是并不执行。</p>
<p>例如:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;My Name&#39;</span><span class="p">,</span>
    <span class="s1">&#39;date&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;My date&#39;</span>
<span class="p">);</span>

<span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="nv">$data</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get_compiled_insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$sql</span><span class="p">;</span>

<span class="c1">// Produces string: INSERT INTO mytable (`title`, `name`, `date`) VALUES (&#39;My title&#39;, &#39;My name&#39;, &#39;My date&#39;)</span>
</pre></div>
</div>
<p>第二个参数用于设置是否重置查询（默认情况下会重置，正如 $this-&gt;db-&gt;insert() 方法一样）:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;My Title&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get_compiled_insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>

<span class="c1">// Produces string: INSERT INTO mytable (`title`) VALUES (&#39;My Title&#39;)</span>

<span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;content&#39;</span><span class="p">,</span> <span class="s1">&#39;My Content&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">get_compiled_insert</span><span class="p">();</span>

<span class="c1">// Produces string: INSERT INTO mytable (`title`, `content`) VALUES (&#39;My Title&#39;, &#39;My Content&#39;)</span>
</pre></div>
</div>
<p>上面的例子中，最值得注意的是，第二个查询并没有用到 <strong>$this-&gt;db-&gt;from()</strong> 方法，
也没有为查询指定表名参数，但是它生成的 SQL 语句中有 INTO mytable 子句。
这是因为查询并没有被重置（使用 <strong>$this-&gt;db-&gt;insert()</strong> 方法会被执行并被重置，
使用 <strong>$this-&gt;db-&gt;reset_query()</strong> 方法直接重置）。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">这个方法不支持批量插入。</p>
</div>
<p><strong>$this-&gt;db-&gt;insert_batch()</strong></p>
<p>该方法根据你提供的数据生成一条 INSERT 语句并执行，它的参数是一个**数组**
或一个**对象**，下面是使用数组的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
        <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Name&#39;</span><span class="p">,</span>
        <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My date&#39;</span>
    <span class="p">),</span>
    <span class="k">array</span><span class="p">(</span>
        <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another title&#39;</span><span class="p">,</span>
        <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another Name&#39;</span><span class="p">,</span>
        <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another date&#39;</span>
    <span class="p">)</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert_batch</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
<span class="c1">// Produces: INSERT INTO mytable (title, name, date) VALUES (&#39;My title&#39;, &#39;My name&#39;, &#39;My date&#39;),  (&#39;Another title&#39;, &#39;Another name&#39;, &#39;Another date&#39;)</span>
</pre></div>
</div>
<p>第一个参数为要插入的表名，第二个参数为要插入的数据，是个二维数组。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有数据会被自动转义，生成安全的查询语句。</p>
</div>
</div>
<div class="section" id="id9">
<h2><a class="toc-backref" href="#id21">更新数据</a><a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;replace()</strong></p>
<p>该方法用于执行一条 REPLACE 语句，REPLACE 语句根据表的**主键**和**唯一索引**
来执行，类似于标准的 DELETE + INSERT 。
使用这个方法，你不用再手工去实现 <tt class="docutils literal"><span class="pre">select()</span></tt>，<tt class="docutils literal"><span class="pre">update()</span></tt>，<tt class="docutils literal"><span class="pre">delete()</span></tt>
以及 <tt class="docutils literal"><span class="pre">insert()</span></tt> 这些方法的不同组合，为你节约大量时间。</p>
<p>例如:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;My Name&#39;</span><span class="p">,</span>
    <span class="s1">&#39;date&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;My date&#39;</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">replace</span><span class="p">(</span><span class="s1">&#39;table&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>

<span class="c1">// Executes: REPLACE INTO mytable (title, name, date) VALUES (&#39;My title&#39;, &#39;My name&#39;, &#39;My date&#39;)</span>
</pre></div>
</div>
<p>上面的例子中，我们假设 <em>title</em> 字段是我们的主键，那么如果我们数据库里有一行
的 <em>title</em> 列的值为 'My title'，这一行将会被删除并被我们的新数据所取代。</p>
<p>也可以使用 <tt class="docutils literal"><span class="pre">set()</span></tt> 方法，而且所有字段都被自动转义，正如 <tt class="docutils literal"><span class="pre">insert()</span></tt> 方法一样。</p>
<p><strong>$this-&gt;db-&gt;set()</strong></p>
<p>该方法用于设置新增或更新的数据。</p>
<p><strong>该方法可以取代直接传递数据数组到 insert 或 update 方法：</strong></p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>  <span class="c1">// Produces: INSERT INTO mytable (`name`) VALUES (&#39;{$name}&#39;)</span>
</pre></div>
</div>
<p>如果你多次调用该方法，它会正确组装出 INSERT 或 UPDATE 语句来:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="nv">$name</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="nv">$title</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;status&#39;</span><span class="p">,</span> <span class="nv">$status</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>set()</strong> 方法也接受可选的第三个参数（<tt class="docutils literal"><span class="pre">$escape</span></tt>），如果设置为 FALSE，数据将不会自动转义。为了说明两者之间的区别，这里有一个带转义的 <tt class="docutils literal"><span class="pre">set()</span></tt> 方法和不带转义的例子。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;field&#39;</span><span class="p">,</span> <span class="s1">&#39;field+1&#39;</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span> <span class="c1">// gives UPDATE mytable SET field = field+1 WHERE id = 2</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;field&#39;</span><span class="p">,</span> <span class="s1">&#39;field+1&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span> <span class="c1">// gives UPDATE `mytable` SET `field` = &#39;field+1&#39; WHERE `id` = 2</span>
</pre></div>
</div>
<p>你也可以传一个关联数组作为参数:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$array</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="nv">$name</span><span class="p">,</span>
    <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$title</span><span class="p">,</span>
    <span class="s1">&#39;status&#39;</span> <span class="o">=&gt;</span> <span class="nv">$status</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="nv">$array</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>或者一个对象:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">class Myclass {</span>
<span class="cm">    public $title = &#39;My Title&#39;;</span>
<span class="cm">    public $content = &#39;My Content&#39;;</span>
<span class="cm">    public $date = &#39;My Date&#39;;</span>
<span class="cm">}</span>
<span class="cm">*/</span>

<span class="nv">$object</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Myclass</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="nv">$object</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;update()</strong></p>
<p>该方法根据你提供的数据生成一条 UPDATE 语句并执行，它的参数是一个 <strong>数组</strong> 或一个 <strong>对象</strong> ，下面是使用数组的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$title</span><span class="p">,</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="nv">$name</span><span class="p">,</span>
    <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="nv">$date</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
<span class="c1">// Produces:</span>
<span class="c1">//</span>
<span class="c1">//  UPDATE mytable</span>
<span class="c1">//  SET title = &#39;{$title}&#39;, name = &#39;{$name}&#39;, date = &#39;{$date}&#39;</span>
<span class="c1">//  WHERE id = $id</span>
</pre></div>
</div>
<p>或者你可以使用一个对象:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">class Myclass {</span>
<span class="cm">    public $title = &#39;My Title&#39;;</span>
<span class="cm">    public $content = &#39;My Content&#39;;</span>
<span class="cm">    public $date = &#39;My Date&#39;;</span>
<span class="cm">}</span>
<span class="cm">*/</span>

<span class="nv">$object</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Myclass</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$object</span><span class="p">);</span>
<span class="c1">// Produces:</span>
<span class="c1">//</span>
<span class="c1">// UPDATE `mytable`</span>
<span class="c1">// SET `title` = &#39;{$title}&#39;, `name` = &#39;{$name}&#39;, `date` = &#39;{$date}&#39;</span>
<span class="c1">// WHERE id = `$id`</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有数据会被自动转义，生成安全的查询语句。</p>
</div>
<p>你应该注意到 $this-&gt;db-&gt;where() 方法的使用，它可以为你设置 WHERE 子句。
你也可以直接使用字符串形式设置 WHERE 子句:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="s2">&quot;id = 4&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>或者使用一个数组:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">));</span>
</pre></div>
</div>
<p>当执行 UPDATE 操作时，你还可以使用上面介绍的 $this-&gt;db-&gt;set() 方法。</p>
<p><strong>$this-&gt;db-&gt;update_batch()</strong></p>
<p>该方法根据你提供的数据生成一条 UPDATE 语句并执行，它的参数是一个**数组**
或一个**对象**，下面是使用数组的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
   <span class="k">array</span><span class="p">(</span>
      <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My title&#39;</span> <span class="p">,</span>
      <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My Name 2&#39;</span> <span class="p">,</span>
      <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;My date 2&#39;</span>
   <span class="p">),</span>
   <span class="k">array</span><span class="p">(</span>
      <span class="s1">&#39;title&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another title&#39;</span> <span class="p">,</span>
      <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another Name 2&#39;</span> <span class="p">,</span>
      <span class="s1">&#39;date&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Another date 2&#39;</span>
   <span class="p">)</span>
<span class="p">);</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">update_batch</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">);</span>

<span class="c1">// Produces:</span>
<span class="c1">// UPDATE `mytable` SET `name` = CASE</span>
<span class="c1">// WHEN `title` = &#39;My title&#39; THEN &#39;My Name 2&#39;</span>
<span class="c1">// WHEN `title` = &#39;Another title&#39; THEN &#39;Another Name 2&#39;</span>
<span class="c1">// ELSE `name` END,</span>
<span class="c1">// `date` = CASE</span>
<span class="c1">// WHEN `title` = &#39;My title&#39; THEN &#39;My date 2&#39;</span>
<span class="c1">// WHEN `title` = &#39;Another title&#39; THEN &#39;Another date 2&#39;</span>
<span class="c1">// ELSE `date` END</span>
<span class="c1">// WHERE `title` IN (&#39;My title&#39;,&#39;Another title&#39;)</span>
</pre></div>
</div>
<p>第一个参数为要更新的表名，第二个参数为要更新的数据，是个二维数组，第三个
参数是 WHERE 语句的键。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">所有数据会被自动转义，生成安全的查询语句。</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">取决于该方法的内部实现，在这个方法之后调用 <tt class="docutils literal"><span class="pre">affected_rows()</span></tt> 方法
返回的结果可能会不正确。但是你可以直接使用该方法的返回值，代表了受影响的行数。</p>
</div>
<p><strong>$this-&gt;db-&gt;get_compiled_update()</strong></p>
<p>该方法和 <tt class="docutils literal"><span class="pre">$this-&gt;db-&gt;get_compiled_insert()</span></tt> 方法完全一样，除了生成的 SQL 语句是
UPDATE 而不是 INSERT。</p>
<p>查看 <cite>$this-&gt;db-&gt;get_compiled_insert()</cite> 方法的文档获取更多信息。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">该方法不支持批量更新。</p>
</div>
</div>
<div class="section" id="id10">
<h2><a class="toc-backref" href="#id22">删除数据</a><a class="headerlink" href="#id10" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;delete()</strong></p>
<p>该方法生成 DELETE 语句并执行。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="nv">$id</span><span class="p">));</span>  <span class="c1">// Produces: // DELETE FROM mytable  // WHERE id = $id</span>
</pre></div>
</div>
<p>第一个参数为表名，第二个参数为 WHERE 条件。你也可以不用第二个参数，
使用 where() 或者 or_where() 函数来替代它:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>

<span class="c1">// Produces:</span>
<span class="c1">// DELETE FROM mytable</span>
<span class="c1">// WHERE id = $id</span>
</pre></div>
</div>
<p>如果你想要从多个表中删除数据，你也可以将由多个表名构成的数组传给 delete() 方法。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$tables</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;table1&#39;</span><span class="p">,</span> <span class="s1">&#39;table2&#39;</span><span class="p">,</span> <span class="s1">&#39;table3&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$tables</span><span class="p">);</span>
</pre></div>
</div>
<p>如果你想要删除一个表中的所有数据，可以使用 truncate() 或 empty_table() 方法。</p>
<p><strong>$this-&gt;db-&gt;empty_table()</strong></p>
<p>该方法生成 DELETE 语句并执行:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">empty_table</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span> <span class="c1">// Produces: DELETE FROM mytable</span>
</pre></div>
</div>
<p><strong>$this-&gt;db-&gt;truncate()</strong></p>
<p>该方法生成 TRUNCATE 语句并执行。</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">truncate</span><span class="p">();</span>

<span class="c1">// or</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">truncate</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>

<span class="c1">// Produce:</span>
<span class="c1">// TRUNCATE mytable</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果 TRUNCATE 语句不可用，truncate() 方法将执行 &quot;DELETE FROM table&quot;。</p>
</div>
<p><strong>$this-&gt;db-&gt;get_compiled_delete()</strong></p>
<p>该方法和 <tt class="docutils literal"><span class="pre">$this-&gt;db-&gt;get_compiled_insert()</span></tt> 方法完全一样，除了生成的 SQL 语句是
DELETE 而不是 INSERT。</p>
<p>查看 <cite>$this-&gt;db-&gt;get_compiled_insert()</cite> 方法的文档获取更多信息。</p>
</div>
<div class="section" id="id11">
<h2><a class="toc-backref" href="#id23">链式方法</a><a class="headerlink" href="#id11" title="永久链接至标题">¶</a></h2>
<p>通过将多个方法连接在一起，链式方法可以大大的简化你的语法。感受一下这个例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">)</span>
        <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">,</span> <span class="nv">$id</span><span class="p">)</span>
        <span class="o">-&gt;</span><span class="na">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
        <span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="ar-caching">
<span id="id12"></span><h2><a class="toc-backref" href="#id24">查询构造器缓存</a><a class="headerlink" href="#ar-caching" title="永久链接至标题">¶</a></h2>
<p>尽管不是 &quot;真正的&quot; 缓存，查询构造器允许你将查询的某个特定部分保存（或 &quot;缓存&quot;）起来，
以便在你的脚本执行之后重用。一般情况下，当查询构造器的一次调用结束后，所有已存储的信息
都会被重置，以便下一次调用。如果开启缓存，你就可以使信息避免被重置，方便你进行重用。</p>
<p>缓存调用是累加的。如果你调用了两次有缓存的 select()，然后再调用两次没有缓存的 select()，
这会导致 select() 被调用4次。</p>
<p>有三个可用的缓存方法方法:</p>
<p><strong>$this-&gt;db-&gt;start_cache()</strong></p>
<p>如需开启缓存必须先调用此方法，所有支持的查询类型（见下文）都会被存储起来供以后使用。</p>
<p><strong>$this-&gt;db-&gt;stop_cache()</strong></p>
<p>此方法用于停止缓存。</p>
<p><strong>$this-&gt;db-&gt;flush_cache()</strong></p>
<p>此方法用于清空缓存。</p>
<p>这里是一个使用缓存的例子:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">start_cache</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;field1&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">stop_cache</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;tablename&#39;</span><span class="p">);</span>
<span class="c1">//Generates: SELECT `field1` FROM (`tablename`)</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;field2&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;tablename&#39;</span><span class="p">);</span>
<span class="c1">//Generates:  SELECT `field1`, `field2` FROM (`tablename`)</span>

<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">flush_cache</span><span class="p">();</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="s1">&#39;field2&#39;</span><span class="p">);</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;tablename&#39;</span><span class="p">);</span>
<span class="c1">//Generates:  SELECT `field2` FROM (`tablename`)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">支持缓存的语句有: select, from, join, where, like, group_by, having, order_by, set</p>
</div>
</div>
<div class="section" id="id13">
<h2><a class="toc-backref" href="#id25">重置查询构造器</a><a class="headerlink" href="#id13" title="永久链接至标题">¶</a></h2>
<p><strong>$this-&gt;db-&gt;reset_query()</strong></p>
<p>该方法无需执行就能重置查询构造器中的查询，$this-&gt;db-&gt;get() 和 $this-&gt;db-&gt;insert()
方法也可以用于重置查询，但是必须要先执行它。和这两个方法一样，使用`查询构造器缓存`_
缓存下来的查询不会被重置。</p>
<p>当你在使用查询构造器生成 SQL 语句（如：<tt class="docutils literal"><span class="pre">$this-&gt;db-&gt;get_compiled_select()</span></tt>），
之后再执行它。这种情况下，不重置查询缓存将非常有用:</p>
<div class="highlight-ci"><div class="highlight"><pre><span class="c1">// Note that the second parameter of the get_compiled_select method is FALSE</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">select</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;field1&#39;</span><span class="p">,</span><span class="s1">&#39;field2&#39;</span><span class="p">))</span>
                <span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;field3&#39;</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
                <span class="o">-&gt;</span><span class="na">get_compiled_select</span><span class="p">(</span><span class="s1">&#39;mytable&#39;</span><span class="p">,</span> <span class="k">FALSE</span><span class="p">);</span>

<span class="c1">// ...</span>
<span class="c1">// Do something crazy with the SQL code... like add it to a cron script for</span>
<span class="c1">// later execution or something...</span>
<span class="c1">// ...</span>

<span class="nv">$data</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">db</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">result_array</span><span class="p">();</span>

<span class="c1">// Would execute and return an array of results of the following query:</span>
<span class="c1">// SELECT field1, field1 from mytable where field3 = 5;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果你正在使用查询构造器缓存功能，连续两次调用 <tt class="docutils literal"><span class="pre">get_compiled_select()</span></tt> 方法
并且不重置你的查询，这将会导致缓存被合并两次。举例来说，例如你正在缓存 <tt class="docutils literal"><span class="pre">select()</span></tt>
方法，那么会查询两个相同的字段。</p>
</div>
</div>
<div class="section" id="class-reference">
<h2><a class="toc-backref" href="#id26">Class Reference</a><a class="headerlink" href="#class-reference" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="CI_DB_query_builder">
<em class="property">class </em><tt class="descname">CI_DB_query_builder</tt><a class="headerlink" href="#CI_DB_query_builder" title="永久链接至目标">¶</a></dt>
<dd><dl class="method">
<dt id="CI_DB_query_builder::reset_query">
<tt class="descname">reset_query</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::reset_query" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Resets the current Query Builder state.  Useful when you want
to build a query that can be cancelled under certain conditions.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::start_cache">
<tt class="descname">start_cache</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::start_cache" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Starts the Query Builder cache.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::stop_cache">
<tt class="descname">stop_cache</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::stop_cache" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Stops the Query Builder cache.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::flush_cache">
<tt class="descname">flush_cache</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::flush_cache" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Empties the Query Builder cache.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::set_dbprefix">
<tt class="descname">set_dbprefix</tt><big>(</big><span class="optional">[</span><em>$prefix = ''</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::set_dbprefix" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$prefix</strong> (<em>string</em>) -- The new prefix to use</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The DB prefix in use</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Sets the database prefix, without having to reconnect.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::dbprefix">
<tt class="descname">dbprefix</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::dbprefix" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- The table name to prefix</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The prefixed table name</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Prepends a database prefix, if one exists in configuration.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::count_all_results">
<tt class="descname">count_all_results</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$reset = TRUE</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::count_all_results" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$reset</strong> (<em>bool</em>) -- Whether to reset values for SELECTs</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">Number of rows in the query result</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">int</p>
</td>
</tr>
</tbody>
</table>
<p>Generates a platform-specific query string that counts
all records returned by an Query Builder query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get">
<tt class="descname">get</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$limit = NULL</em><span class="optional">[</span>, <em>$offset = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- The table to query</li>
<li><strong>$limit</strong> (<em>int</em>) -- The LIMIT clause</li>
<li><strong>$offset</strong> (<em>int</em>) -- The OFFSET clause</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_result instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_result</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and runs SELECT statement based on the already
called Query Builder methods.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get_where">
<tt class="descname">get_where</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$where = NULL</em><span class="optional">[</span>, <em>$limit = NULL</em><span class="optional">[</span>, <em>$offset = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get_where" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>mixed</em>) -- The table(s) to fetch data from; string or array</li>
<li><strong>$where</strong> (<em>string</em>) -- The WHERE clause</li>
<li><strong>$limit</strong> (<em>int</em>) -- The LIMIT clause</li>
<li><strong>$offset</strong> (<em>int</em>) -- The OFFSET clause</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_result instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_result</p>
</td>
</tr>
</tbody>
</table>
<p>Same as <tt class="docutils literal"><span class="pre">get()</span></tt>, but also allows the WHERE to be added directly.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::select">
<tt class="descname">select</tt><big>(</big><span class="optional">[</span><em>$select = '*'</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::select" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$select</strong> (<em>string</em>) -- The SELECT portion of a query</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a SELECT clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::select_avg">
<tt class="descname">select_avg</tt><big>(</big><span class="optional">[</span><em>$select = ''</em><span class="optional">[</span>, <em>$alias = ''</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::select_avg" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$select</strong> (<em>string</em>) -- Field to compute the average of</li>
<li><strong>$alias</strong> (<em>string</em>) -- Alias for the resulting value name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a SELECT AVG(field) clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::select_max">
<tt class="descname">select_max</tt><big>(</big><span class="optional">[</span><em>$select = ''</em><span class="optional">[</span>, <em>$alias = ''</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::select_max" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$select</strong> (<em>string</em>) -- Field to compute the maximum of</li>
<li><strong>$alias</strong> (<em>string</em>) -- Alias for the resulting value name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a SELECT MAX(field) clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::select_min">
<tt class="descname">select_min</tt><big>(</big><span class="optional">[</span><em>$select = ''</em><span class="optional">[</span>, <em>$alias = ''</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::select_min" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$select</strong> (<em>string</em>) -- Field to compute the minimum of</li>
<li><strong>$alias</strong> (<em>string</em>) -- Alias for the resulting value name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a SELECT MIN(field) clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::select_sum">
<tt class="descname">select_sum</tt><big>(</big><span class="optional">[</span><em>$select = ''</em><span class="optional">[</span>, <em>$alias = ''</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::select_sum" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$select</strong> (<em>string</em>) -- Field to compute the sum of</li>
<li><strong>$alias</strong> (<em>string</em>) -- Alias for the resulting value name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a SELECT SUM(field) clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::distinct">
<tt class="descname">distinct</tt><big>(</big><span class="optional">[</span><em>$val = TRUE</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::distinct" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$val</strong> (<em>bool</em>) -- Desired value of the &quot;distinct&quot; flag</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Sets a flag which tells the query builder to add
a DISTINCT clause to the SELECT portion of the query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::from">
<tt class="descname">from</tt><big>(</big><em>$from</em><big>)</big><a class="headerlink" href="#CI_DB_query_builder::from" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$from</strong> (<em>mixed</em>) -- Table name(s); string or array</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Specifies the FROM clause of a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::join">
<tt class="descname">join</tt><big>(</big><em>$table</em>, <em>$cond</em><span class="optional">[</span>, <em>$type = ''</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::join" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name to join</li>
<li><strong>$cond</strong> (<em>string</em>) -- The JOIN ON condition</li>
<li><strong>$type</strong> (<em>string</em>) -- The JOIN type</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a JOIN clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::where">
<tt class="descname">where</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::where" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Name of field to compare, or associative array</li>
<li><strong>$value</strong> (<em>mixed</em>) -- If a single key, compared to this value</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates the WHERE portion of the query.</dt>
<dd>Separates multiple calls with 'AND'.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_where">
<tt class="descname">or_where</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_where" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Name of field to compare, or associative array</li>
<li><strong>$value</strong> (<em>mixed</em>) -- If a single key, compared to this value</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates the WHERE portion of the query.</dt>
<dd>Separates multiple calls with 'OR'.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_where_in">
<tt class="descname">or_where_in</tt><big>(</big><span class="optional">[</span><em>$key = NULL</em><span class="optional">[</span>, <em>$values = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_where_in" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>string</em>) -- The field to search</li>
<li><strong>$values</strong> (<em>array</em>) -- The values searched on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates a WHERE field IN('item', 'item') SQL query,</dt>
<dd>joined with 'OR' if appropriate.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_where_not_in">
<tt class="descname">or_where_not_in</tt><big>(</big><span class="optional">[</span><em>$key = NULL</em><span class="optional">[</span>, <em>$values = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_where_not_in" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>string</em>) -- The field to search</li>
<li><strong>$values</strong> (<em>array</em>) -- The values searched on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates a WHERE field NOT IN('item', 'item') SQL query,</dt>
<dd>joined with 'OR' if appropriate.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::where_in">
<tt class="descname">where_in</tt><big>(</big><span class="optional">[</span><em>$key = NULL</em><span class="optional">[</span>, <em>$values = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::where_in" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>string</em>) -- Name of field to examine</li>
<li><strong>$values</strong> (<em>array</em>) -- Array of target values</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates a WHERE field IN('item', 'item') SQL query,</dt>
<dd>joined with 'AND' if appropriate.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::where_not_in">
<tt class="descname">where_not_in</tt><big>(</big><span class="optional">[</span><em>$key = NULL</em><span class="optional">[</span>, <em>$values = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::where_not_in" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>string</em>) -- Name of field to examine</li>
<li><strong>$values</strong> (<em>array</em>) -- Array of target values</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">DB_query_builder instance</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">object</p>
</td>
</tr>
</tbody>
</table>
<dl class="docutils">
<dt>Generates a WHERE field NOT IN('item', 'item') SQL query,</dt>
<dd>joined with 'AND' if appropriate.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::group_start">
<tt class="descname">group_start</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::group_start" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Starts a group expression, using ANDs for the conditions inside it.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_group_start">
<tt class="descname">or_group_start</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_group_start" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Starts a group expression, using ORs for the conditions inside it.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::not_group_start">
<tt class="descname">not_group_start</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::not_group_start" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Starts a group expression, using AND NOTs for the conditions inside it.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_not_group_start">
<tt class="descname">or_not_group_start</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_not_group_start" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">CI_DB_query_builder instance (method chaining)</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">CI_DB_query_builder</td>
</tr>
</tbody>
</table>
<p>Starts a group expression, using OR NOTs for the conditions inside it.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::group_end">
<tt class="descname">group_end</tt><big>(</big><big>)</big><a class="headerlink" href="#CI_DB_query_builder::group_end" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">返回:</th><td class="field-body">DB_query_builder instance</td>
</tr>
<tr class="field-even field"><th class="field-name">返回类型:</th><td class="field-body">object</td>
</tr>
</tbody>
</table>
<p>Ends a group expression.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::like">
<tt class="descname">like</tt><big>(</big><em>$field</em><span class="optional">[</span>, <em>$match = ''</em><span class="optional">[</span>, <em>$side = 'both'</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::like" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$field</strong> (<em>string</em>) -- Field name</li>
<li><strong>$match</strong> (<em>string</em>) -- Text portion to match</li>
<li><strong>$side</strong> (<em>string</em>) -- Which side of the expression to put the '%' wildcard on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a LIKE clause to a query, separating multiple calls with AND.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_like">
<tt class="descname">or_like</tt><big>(</big><em>$field</em><span class="optional">[</span>, <em>$match = ''</em><span class="optional">[</span>, <em>$side = 'both'</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_like" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$field</strong> (<em>string</em>) -- Field name</li>
<li><strong>$match</strong> (<em>string</em>) -- Text portion to match</li>
<li><strong>$side</strong> (<em>string</em>) -- Which side of the expression to put the '%' wildcard on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a LIKE clause to a query, separating multiple class with OR.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::not_like">
<tt class="descname">not_like</tt><big>(</big><em>$field</em><span class="optional">[</span>, <em>$match = ''</em><span class="optional">[</span>, <em>$side = 'both'</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::not_like" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$field</strong> (<em>string</em>) -- Field name</li>
<li><strong>$match</strong> (<em>string</em>) -- Text portion to match</li>
<li><strong>$side</strong> (<em>string</em>) -- Which side of the expression to put the '%' wildcard on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a NOT LIKE clause to a query, separating multiple calls with AND.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_not_like">
<tt class="descname">or_not_like</tt><big>(</big><em>$field</em><span class="optional">[</span>, <em>$match = ''</em><span class="optional">[</span>, <em>$side = 'both'</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_not_like" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$field</strong> (<em>string</em>) -- Field name</li>
<li><strong>$match</strong> (<em>string</em>) -- Text portion to match</li>
<li><strong>$side</strong> (<em>string</em>) -- Which side of the expression to put the '%' wildcard on</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a NOT LIKE clause to a query, separating multiple calls with OR.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::having">
<tt class="descname">having</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::having" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Identifier (string) or associative array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Value sought if $key is an identifier</li>
<li><strong>$escape</strong> (<em>string</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a HAVING clause to a query, separating multiple calls with AND.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::or_having">
<tt class="descname">or_having</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::or_having" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Identifier (string) or associative array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Value sought if $key is an identifier</li>
<li><strong>$escape</strong> (<em>string</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a HAVING clause to a query, separating multiple calls with OR.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::group_by">
<tt class="descname">group_by</tt><big>(</big><em>$by</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::group_by" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$by</strong> (<em>mixed</em>) -- Field(s) to group by; string or array</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds a GROUP BY clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::order_by">
<tt class="descname">order_by</tt><big>(</big><em>$orderby</em><span class="optional">[</span>, <em>$direction = ''</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::order_by" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$orderby</strong> (<em>string</em>) -- Field to order by</li>
<li><strong>$direction</strong> (<em>string</em>) -- The order requested - ASC, DESC or random</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds an ORDER BY clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::limit">
<tt class="descname">limit</tt><big>(</big><em>$value</em><span class="optional">[</span>, <em>$offset = 0</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::limit" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$value</strong> (<em>int</em>) -- Number of rows to limit the results to</li>
<li><strong>$offset</strong> (<em>int</em>) -- Number of rows to skip</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds LIMIT and OFFSET clauses to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::offset">
<tt class="descname">offset</tt><big>(</big><em>$offset</em><big>)</big><a class="headerlink" href="#CI_DB_query_builder::offset" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$offset</strong> (<em>int</em>) -- Number of rows to skip</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds an OFFSET clause to a query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::set">
<tt class="descname">set</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = ''</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::set" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Field name, or an array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Field value, if $key is a single field</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds field/value pairs to be passed later to <tt class="docutils literal"><span class="pre">insert()</span></tt>,
<tt class="docutils literal"><span class="pre">update()</span></tt> or <tt class="docutils literal"><span class="pre">replace()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::insert">
<tt class="descname">insert</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$set = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::insert" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$set</strong> (<em>array</em>) -- An associative array of field/value pairs</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">bool</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes an INSERT statement.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::insert_batch">
<tt class="descname">insert_batch</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$set = NULL</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::insert_batch" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$set</strong> (<em>array</em>) -- Data to insert</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">Number of rows inserted or FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes batch INSERT statements.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::set_insert_batch">
<tt class="descname">set_insert_batch</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = ''</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::set_insert_batch" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Field name or an array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Field value, if $key is a single field</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds field/value pairs to be inserted in a table later via <tt class="docutils literal"><span class="pre">insert_batch()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::update">
<tt class="descname">update</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$set = NULL</em><span class="optional">[</span>, <em>$where = NULL</em><span class="optional">[</span>, <em>$limit = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::update" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$set</strong> (<em>array</em>) -- An associative array of field/value pairs</li>
<li><strong>$where</strong> (<em>string</em>) -- The WHERE clause</li>
<li><strong>$limit</strong> (<em>int</em>) -- The LIMIT clause</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">bool</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes an UPDATE statement.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::update_batch">
<tt class="descname">update_batch</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$set = NULL</em><span class="optional">[</span>, <em>$value = NULL</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::update_batch" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$set</strong> (<em>array</em>) -- Field name, or an associative array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Field value, if $set is a single field</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">Number of rows updated or FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes batch UPDATE statements.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::set_update_batch">
<tt class="descname">set_update_batch</tt><big>(</big><em>$key</em><span class="optional">[</span>, <em>$value = ''</em><span class="optional">[</span>, <em>$escape = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::set_update_batch" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$key</strong> (<em>mixed</em>) -- Field name or an array of field/value pairs</li>
<li><strong>$value</strong> (<em>string</em>) -- Field value, if $key is a single field</li>
<li><strong>$escape</strong> (<em>bool</em>) -- Whether to escape values and identifiers</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining)</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">CI_DB_query_builder</p>
</td>
</tr>
</tbody>
</table>
<p>Adds field/value pairs to be updated in a table later via <tt class="docutils literal"><span class="pre">update_batch()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::replace">
<tt class="descname">replace</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$set = NULL</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::replace" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$set</strong> (<em>array</em>) -- An associative array of field/value pairs</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">bool</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes a REPLACE statement.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::delete">
<tt class="descname">delete</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$where = ''</em><span class="optional">[</span>, <em>$limit = NULL</em><span class="optional">[</span>, <em>$reset_data = TRUE</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::delete" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>mixed</em>) -- The table(s) to delete from; string or array</li>
<li><strong>$where</strong> (<em>string</em>) -- The WHERE clause</li>
<li><strong>$limit</strong> (<em>int</em>) -- The LIMIT clause</li>
<li><strong>$reset_data</strong> (<em>bool</em>) -- TRUE to reset the query &quot;write&quot; clause</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">CI_DB_query_builder instance (method chaining) or FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">mixed</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles and executes a DELETE query.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::truncate">
<tt class="descname">truncate</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::truncate" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">bool</p>
</td>
</tr>
</tbody>
</table>
<p>Executes a TRUNCATE statement on a table.</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">If the database platform in use doesn't support TRUNCATE,
a DELETE statement will be used instead.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::empty_table">
<tt class="descname">empty_table</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::empty_table" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">TRUE on success, FALSE on failure</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">bool</p>
</td>
</tr>
</tbody>
</table>
<p>Deletes all records from a table via a DELETE statement.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get_compiled_select">
<tt class="descname">get_compiled_select</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$reset = TRUE</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get_compiled_select" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$reset</strong> (<em>bool</em>) -- Whether to reset the current QB values or not</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The compiled SQL statement as a string</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles a SELECT statement and returns it as a string.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get_compiled_insert">
<tt class="descname">get_compiled_insert</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$reset = TRUE</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get_compiled_insert" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$reset</strong> (<em>bool</em>) -- Whether to reset the current QB values or not</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The compiled SQL statement as a string</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles an INSERT statement and returns it as a string.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get_compiled_update">
<tt class="descname">get_compiled_update</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$reset = TRUE</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get_compiled_update" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$reset</strong> (<em>bool</em>) -- Whether to reset the current QB values or not</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The compiled SQL statement as a string</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles an UPDATE statement and returns it as a string.</p>
</dd></dl>

<dl class="method">
<dt id="CI_DB_query_builder::get_compiled_delete">
<tt class="descname">get_compiled_delete</tt><big>(</big><span class="optional">[</span><em>$table = ''</em><span class="optional">[</span>, <em>$reset = TRUE</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#CI_DB_query_builder::get_compiled_delete" title="永久链接至目标">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first simple">
<li><strong>$table</strong> (<em>string</em>) -- Table name</li>
<li><strong>$reset</strong> (<em>bool</em>) -- Whether to reset the current QB values or not</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first">The compiled SQL statement as a string</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">返回类型:</th><td class="field-body"><p class="first last">string</p>
</td>
</tr>
</tbody>
</table>
<p>Compiles a DELETE statement and returns it as a string.</p>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="transactions.html" class="btn btn-neutral float-right" title="事务">下一个主题 <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="helpers.html" class="btn btn-neutral" title="查询辅助函数"><span class="fa fa-arrow-circle-left"></span> 上一个主题</a>
      
    </div>
  

  
  <div id="notes">
  
  <!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="userguide3/database/query_builder" data-title="查询构造器类" data-url="http://codeigniter.org.cn/user_guide/database/query_builder.html"></div>
  <!-- 多说评论框 end -->
  

  <div class="ds-share flat" data-thread-key="userguide3/database/query_builder" data-title="查询构造器类 &mdash; CodeIgniter 3.0.0 中文手册|用户手册|用户指南|中文文档" data-url="http://codeigniter.org.cn/user_guide/database/query_builder.html" data-images="" data-content="">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">

        <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
  </div>

  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
  <script type="text/javascript">
    var duoshuoQuery = {short_name:"codeigniter"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0]
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
  <!-- 多说公共JS代码 end -->
  </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; 版权所有 2014 - 2015, 不列颠哥伦比亚理工学院.
       最后修改: 2015-10-11.
    </p>

    <p>以 <a href="http://www.aneasystone.com/" target="_blank">aneasystone</a> 制作的手册为基础构建</p>
  </div>

  
    <p>基于 <a href="http://sphinx-doc.org/">Sphinx</a> 并使用 <a href="https://readthedocs.org">Read the Docs</a> 提供的<a href="https://github.com/snide/sphinx_rtd_theme">风格</a>构建</p>
    <p>
        <a href="https://github.com/CodeIgniter-Chinese/codeigniter-user-guide" target="_blank">Github 简体中文翻译</a>
        ·
        <a href="http://codeigniter-chinese.github.io/codeigniter-user-guide/codeigniter_user_guide.zip">离线版压缩包下载</a>
        ·
        <a href="http://codeigniter-chinese.github.io/codeigniter-user-guide/CodeIgniter.pdf">PDF 版下载</a>
    </p>
  

</footer>
        </div>
      </div>

    </section>

  </div>
  

  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'3.0.2',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  false
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="../_static/translations.js"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

  
    <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-272152-3', 'auto');
        ga('send', 'pageview');
    </script>
  

</body>
</html>